import { createRouter, createWebHistory } from 'vue-router';
import { constantRoutes, notFountRoute } from './constantRoutes';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

export const addAsyncRoutes = (routes: RouteRecordRaw[] = []) => {
  routes.forEach((route) => {
    router.addRoute(route);
  });
  // 添加404
  router.addRoute(notFountRoute);
};

export default router;
